<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\User;
use api\wxapp\model\Shop;
use api\wxapp\model\ShopGoods;
use api\wxapp\model\Problem;
use api\wxapp\model\Order;
use api\wxapp\model\OrderList;
use api\wxapp\model\Goods;
use api\wxapp\model\Integral;
class UserController extends ApiBaseController
{
    public $params = array();
    public function initialize()
    {
        parent::initialize();
        $this->params = $this->getParam();
        $this->checkuid($this->params);

    }
    public function index()
    {
        $info = User::get($this->params['uid']);
        $info['shopname'] = Shop::where('id',$info['shop_id'])->value('name');
        $this->success('请求成功!',$info);
    }
    //我的积分副本--积分记录
    public function integral()
    {
        $where[] = ['uid','eq',$this->params['uid']];
        if(isset($this->params['status'])){
            $where[] = ['status','eq',$this->params['status']];
        }
        $list = Integral::where($where)->select();
        $this->success('请求成功!',$list);
    }
    //我的积分副本--积分余额
    public function myin()
    {
        $uid = $this->params['uid'];
        $info = User::where('id',$uid)->field('img,nickname,integral')->find();
        $info['total'] = Integral::where(['uid'=>$uid,'status'=>1])->SUM('integral');
        $this->success('请求成功!',$info);
    }
    //积分排名
    public function scoreorder(User $user)
    {
        $query = $user->returnfield($user);
        $list = $query->order('integral desc')->select();
        $this->success('请求成功!',$list);
    }
    //所有订单
    public function orderAll()
    {
        $params = $this->params;
        $where[] = ['uid','eq',$params['uid']];
        if(isset($params['status'])){
            if($params['status'] == 1 || $params['status'] == 2){
                $where[] = ['status','eq',$params['status']];
            }else{
                $where[] = ['status','eq',3];
            }
        }
        $list = Order::where($where)
            ->field('pay_price,id,is_coupon,ordernum,score,status,shop_id')
            ->all();
        foreach($list as $k=>$v){
            $idarr = OrderList::where('order_id',$v['id'])->all();
            $arr = array();
            foreach($idarr as $kk=>$vv){
                $info = ShopGoods::where(['shop_id'=>$v['shop_id'],'goods_id'=>$vv['goods_id']])->find();
                if(isset($params['status'])){
                    if($params['status'] == 3 && $info){//待收货
                        unset($idarr[$kk]);
                        continue;
                    }
                    if($params['status'] == 4 && !$info){//待自提
                        unset($idarr[$kk]);
                        continue;
                    }
                }
                if($info){
                    $goods['lable'] = '自提';
                }else{
                    $goods['lable'] = '直邮';
                }
                $find = Goods::get($vv['goods_id']);
                $goods['img'] = $find['img'];
                $goods['name'] = $find['name'];
                if($v['score']){//积分订单
                    $goods['score'] = $v['score'];
                }
                $goods['price'] = $vv['price'];
                $goods['num'] = $vv['num'];
                $arr[] = $goods;
            }
            $list[$k]['goods'] = $arr;
            if(!$list[$k]['goods']){
                unset($list[$k]);
            }
        }
        $this->success('请求成功!',$list);
    }

    //修改我的信息
    public function infoput()
    {
        $this->checkuid($this->params);
        !isset($this->params['nickname']) ? $this->error('参数缺失!!!!') : '';
        !isset($this->params['img']) ? $this->error('参数缺失!!!!') : '';
        !isset($this->params['phone']) ? $this->error('参数缺失!!!!') : '';
        !isset($this->params['sex']) ? $this->error('参数缺失!!!!') : '';
        !isset($this->params['birthday']) ? $this->error('参数缺失!!!!') : '';
        $id = $this->params['uid'];
        $data = $this->params;
        unset($data['uid']);
        $data['id'] = $id;
        $res = User::update($data);
        if($res){
            $this->success('请求成功!');
        }else{
            $this->error('请求失败!!!!');
        }
    }
    //我得到报名
    public function sign()
    {
        $params = $this->params;
        $where[] = ['t1.uid','eq',$params['uid']];
        if(isset($params['status'])){
            $where[] = ['t2.status','eq',$params['status']];
        }else{
            $where[] = ['t2.status','eq',2];
        }
        $list = db::name('app_activity_list')->alias('t1')
            ->join('app_activity t2','t2.id = t1.activity_id')
            ->field('t1.id,t1.uid,t2.name,t2.content,t2.img,t2.city,t2.num')
            ->where($where)
            ->select()->toArray();
        foreach($list as $k=>$v){
            $list[$k]['count'] = db::name('app_activity_list')->where('activity_id',$v['id'])->count();
        }
        $this->success('请求成功!',$list);
    }

    //常见问题
    public function problem()
    {
        $list = Problem::all();
        $this->success('请求成功!',$list);
    }
}
